package com.example.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Enumeration;

@WebServlet(name = "first", urlPatterns = {"/first"},
loadOnStartup = 1
)
public class FirstServlet extends HttpServlet {

    private Integer hit = 128;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        resp.setCharacterEncoding("utf-8");
//        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain;charset=utf-8");
        System.out.println("你好222222");
        String hellow = req.getParameter("hello");
        resp.getWriter().println(hellow + ": 你好啊222333");
        resp.getWriter().println(new Date());
        resp.getWriter().println(System.currentTimeMillis());

        String name = req.getParameter("name");
        resp.getWriter().println(name);
        String names[] = req.getParameterValues("name");
        resp.getWriter().println(names.length);

        String num = req.getParameter("num");
        Integer n = Integer.parseInt(num);
        if(n > hit) {
            resp.getWriter().println("big");
        } else if(n < hit) {
            resp.getWriter().println("small");
        } else {
            resp.getWriter().println("Congratulations, you got it.");
        }

        //
        Enumeration<String> headerKeys = req.getHeaderNames();
        while(headerKeys.hasMoreElements()) {
            String key = headerKeys.nextElement();
            String value = req.getHeader(key);
            resp.getWriter().println(String.format("%s=%s", key, value));
        }
        resp.getWriter().println(String.format("----------------------------------"));
        Enumeration<String> paramKeys = req.getParameterNames();
        while(paramKeys.hasMoreElements()) {
            String key = paramKeys.nextElement();
            String value = req.getParameter(key);
            resp.getWriter().println(String.format("%s=%s", key, value));
        }
        resp.getWriter().println(String.format("----------------------------------"));
        req.setAttribute("name", name);
        Enumeration<String> attributeKeys = req.getAttributeNames();
        while(attributeKeys.hasMoreElements()) {
            String key = attributeKeys.nextElement();
            Object value = req.getAttribute(key);
            resp.getWriter().println(String.format("%s=%s", key, value));
        }


//        req.setAttribute("name", name);
//
//        req.getAttribute("name");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("你好");
        resp.getWriter().println("do post method invocation");
        resp.getWriter().println(new Date());
        resp.getWriter().println(System.currentTimeMillis());
    }

    @Override
    public void init() throws ServletException {
        System.out.println("Init method invoked.");
    }

    @Override
    public void destroy() {
        System.out.println("Destroy method invoked .");
    }
}
